package org.csu.mypetstore.controller;

import org.apache.catalina.Manager;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class SearchProductController {
    @Autowired
    CatalogService catalogService;

    @Autowired
    HttpSession session;

    @Autowired
    OrderService orderService;

    @GetMapping("/viewProductId")
    public String search( String keyword, Model model){

        List<Product> productList = catalogService.searchProductList(keyword);
        model.addAttribute("productList",productList);

        return "catalog/SearchProducts";
    }

    @GetMapping("/searchProduct")
    public String searchProduct( String keyword, Model model){

        List<Product> productList = catalogService.searchProductList(keyword);
        model.addAttribute("productList",productList);

        return "manage/catalog/ManageProduct";
    }

    @GetMapping("/searchItem")
    public String searchItem( String keyword, Model model){
//        Product product = catalogService.getProduct(productId);
        List<Item> itemList = catalogService.searchItemList(keyword);
//        model.addAttribute("product",product);
        model.addAttribute("itemList",itemList);

        return "manage/catalog/ManageItem";
    }

    @GetMapping("/searchCategory")
    public String searchCategory( String keyword, Model model){
        List<Category> categoryList = catalogService.searchCategoryList(keyword);
        session.setAttribute("categoryList",categoryList);


        return "manage/catalog/ManageCategory";
    }

    @GetMapping("/searchOrderList")
    public String searchOrderList(String keyword,Model model){
        List<Order> orderList = orderService.searchOrderList(keyword);
        session.setAttribute("allOrderList",orderList);
        return "manage/order/orderManage";
    }

    @GetMapping("/findResultServlet")
    public void findResultServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取搜索框输入的内容
        String keyword = request.getParameter("keyword");
        System.out.println(keyword);
        //向server层调用相应的业务
        List<Product> productList = catalogService.searchProductList(keyword);

        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();

        //返回结果
        String res = "";
        for(int i=0; i<productList.size(); i++){
            if(i>0){
                res += "," + productList.get(i).getName();
                System.out.println(productList.get(i).getName());
            }else{
                res += productList.get(i).getName();
            }
        }

        out.write(res);

        out.flush();
        out.close();
    }

}
